<template>
  <div class="dialog">
    <el-dialog
      :title="dialog.title"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :modal-append-to-body="false"
      :visible.sync="dialog.show"
    >
      <el-form
        ref="ServerGroupForms"
        :model="ServerGroupForms"
        label-width="150px"
      >
        <el-form-item v-if="false" label="Id">
          <el-input
            v-model.number="ServerGroupForms.id"
            placeholder="请输入服务器组ID"
          ></el-input>
        </el-form-item>
        <el-form-item label="服务器组Id">
          <el-input
            v-model.number="ServerGroupForms.serverGroupId"
            placeholder="请输入服务器组ID"
          ></el-input>
        </el-form-item>

        <el-form-item label="服务器组名称:">
          <el-input
            v-model="ServerGroupForms.serverGroupName"
            placeholder="请输入服务器名称"
          ></el-input>
        </el-form-item>

        <el-form-item label="服务器Id：">
          <el-input
            v-model="ServerGroupForms.serverId"
            placeholder="请输入服务器Id以逗号隔开"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleclose()">取 消</el-button>
        <el-button type="primary" @click="handleAddEdit()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "ServerGroupDialog",
  // 获取父亲传过来的值
  props: {
    dialog: {
      type: Object,
      default: () => {},
    },
    ServerGroupForm: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      ServerGroupForms: this.ServerGroupForm,
    };
  },

  methods: {
    handleclose() {
      this.dialog.show = false;
    },
    getTime(timedate){
      var date = new Date(timedate);
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? ('0' + m) : m;
      var d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      var h = date.getHours();
      var minute = date.getMinutes();
      minute = minute < 10 ? ('0' + minute) : minute;
      let time = y + '-' + m + '-' + d+' '+h+':'+minute;
      return time;
    },
    handleAddEdit() {
       if(this.dialog.title=="新增服务器组"){
          if (this.ServerGroupForms.serverGroupId == "") {
            this.$message({type:"warning",message:"请输入服务器组id"});  
            return;
          } else if (this.ServerGroupForms.serverGroupName == "") {
            this.$message({type:"warning",message:"请输入服务器组名称"});  
            return;
          } else if (this.ServerGroupForms.serverId == "") {
            this.$message({type:"warning",message:"请输入服务器id"});  
            return;
          } else {
            var serverIdsarr = this.ServerGroupForms.serverId.split(",");
            let serverIds = [];
            for (var k = 0; k < serverIdsarr.length; k++) {
              if (serverIdsarr[k] == 0) {
                serverIdsarr[k] = "";
              } else {
                serverIds.push(Number(serverIdsarr[k]));
              }
            }
            let obj = {
              id: this.ServerGroupForms.id,
              serverGroupId: this.ServerGroupForms.serverGroupId,
              serverGroupName: this.ServerGroupForms.serverGroupName,
              serverIds: serverIds,
            };
            this.$emit("saveServerGroup", obj);
            this.dialog.show = false;
          }
      }else{
          if (this.ServerGroupForms.serverGroupId == ""||this.ServerGroupForms.serverGroupId==undefined) {
            this.$message({type:"warning",message:"请输入服务器组id"});  
            return;
          } else if (this.ServerGroupForms.serverGroupName == ""||this.ServerGroupForms.serverGroupName==undefined) {
            this.$message({type:"warning",message:"请输入服务器组名称"});  
            return;
          } else if (this.ServerGroupForms.serverId == ""||this.ServerGroupForms.serverId==undefined) {
            this.$message({type:"warning",message:"请输入服务器id"});  
            return;
          } else {
            var serverIdsarr = this.ServerGroupForms.serverId.split(",");
            let serverIds = [];
            for (var k = 0; k < serverIdsarr.length; k++) {
              if (serverIdsarr[k] == 0) {
                serverIdsarr[k] = "";
              } else {
                serverIds.push(Number(serverIdsarr[k]));
              }
            }
            let obj = {
              id: this.ServerGroupForms.id,
              serverGroupId: this.ServerGroupForms.serverGroupId,
              serverGroupName: this.ServerGroupForms.serverGroupName,
              serverIds: serverIds,
            };
            this.$emit("saveServerGroup", obj);
            this.dialog.show = false;
          }
       }
    },
  },
};
</script>
<style scoped>
.upload {
  width: 20px;
  height: 20px;
  margin-top: 10px;
}
.btn {
  padding: 10px;
  margin-left: 50%;
}
</style>